package com.fling.input;

import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;
import com.fling.Constants;
import com.fling.objects.Hero;
import com.fling.screen.GameScreen;

public class InputHandler implements GestureListener, Constants {
	private Hero hero;
	private GameScreen screen;
	private boolean touched;

	public InputHandler(Hero hero, GameScreen screen) {
		this.hero = hero;
		this.screen = screen;
	}

	@Override
	public boolean touchDown(float x, float y, int pointer, int button) {
		// hero climbs the wall
		if (pointer < 1) {
			touched=true;
			hero.climb();
			screen.getTutorial().climb();
		}
		return false;
	}

	@Override
	public boolean fling(float velocityX, float velocityY, int button) {
		// hero jumps to other side
		if (touched && !screen.isPause() && !screen.isRestart()) {
			Vector2 dir = new Vector2(velocityX, velocityY);
			dir.nor();
			if (dir.x > Math.sqrt(2) / 2) {
				hero.jumpRight();
				screen.getTutorial().jumpRight();
			}
			if (dir.y > Math.sqrt(2) / 2) {
				hero.jumpDown();
				screen.getTutorial().jumpDown();
			}
			if (dir.x < -Math.sqrt(2) / 2) {
				hero.jumpLeft();
				screen.getTutorial().jumpLeft();
			}
			if (dir.y < -Math.sqrt(2) / 2) {
				hero.jumpUp();
				screen.getTutorial().jumpUp();
			}
			touched=false;
		}
		return false;
	}

	@Override
	public boolean panStop(float x, float y, int pointer, int button) {
		hero.fall();
		return false;
	}

	@Override
	public boolean tap(float x, float y, int count, int button) {
		if (touched) {
			// stop climbing
			hero.fall();
			screen.getTutorial().fall();
			touched=false;
		}
		return false;
	}

	@Override
	public boolean longPress(float x, float y) {
		return false;
	}

	@Override
	public boolean pan(float x, float y, float deltaX, float deltaY) {
		return false;
	}

	@Override
	public boolean zoom(float initialDistance, float distance) {
		return false;
	}

	@Override
	public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
		return false;
	}

}